<?php

namespace plugins\logistics\controller;

use cmf\controller\PluginBaseController;//引用插件基类;
use plugins\logistics\model\LogisticsKdniaoModel;
use think\Db;
use plugins\store\Common;

/**
 * api控制器
 */
class ApiLogisticsKdniaoController extends PluginBaseController
{


    /**
     * 查询物流
     * @param  $isModule
     * @param  $ShipperCode   快递公司编码
     * @param  $LogisticCode  物流单号
     * @param  $store  店铺id
     * @return \返回json字符串
     */
    function OrderTraces( $ShipperCode='',$LogisticCode='', $store=''){
        $param = $this->request->post();
        $KdApi = new LogisticsKdniaoModel;
//取订单商品物流单号
//查询物流公司单号
      
        if(!empty($param['order_num'])){
                
            $data = db('logistics_order')->where('order_num',$param['order_num'])->find();

        }           
        $orderInfo['ShipperCode']= isset($data['code']) ? $data['code'] : $ShipperCode ;
        $orderInfo['LogisticCode']= isset($data['logistics_code']) ? $data['logistics_code'] : $LogisticCode ;
 
        if(empty($orderInfo['ShipperCode']) || empty($orderInfo['LogisticCode']) || empty($store)) return api_json_echo(false,'缺少条件','',100);
        

            $EXinfo = db('logistics_confign')->where('store_id',$store)->find();
            if(empty($EXinfo)){
                $EXinfo = db('logistics_confign')->where('store_id','admin')->find();
            }

            $where = ['ShipperCode'=>$orderInfo['ShipperCode'],'LogisticCode'=>$orderInfo['LogisticCode']];
            $logistics = db('logistics_record')->where($where)->find();  
            //判断是否有记录
            if($logistics){//有记录 
            $time = intval( (time()-$logistics['add_time'] )/3600);//相差时间

            if($logistics['State']==3 || $time<2){//物流已签收或两小时内更新过
                $msg = json_decode($logistics['logistics'],true);
            }else{
                //获取物流信息
                $data = $KdApi->getOrderTracesByJson($orderInfo['ShipperCode'],$orderInfo["LogisticCode"],$EXinfo);
                $msg = json_decode($data,true);
                if($msg['Success']==true){
                    db('logistics_record')->where($where)->update(['logistics'=>$data,'State'=>$msg['State']]);//更新物流信息
                }
            }      

            }else{
                //获取物流信息
                $data = $KdApi->getOrderTracesByJson($orderInfo['ShipperCode'],$orderInfo["LogisticCode"],$EXinfo);
                $time = time();
                $msg = json_decode($data,true);
                if($msg['Success']==true){
                    $time = time();
                    db('logistics_record')->insert(['ShipperCode'=>$orderInfo['ShipperCode'],'LogisticCode'=>$orderInfo['LogisticCode'],'logistics'=>$data,'add_time'=>$time,'State'=>$msg['State']]);//保存物流信息                       
                }
            } 
            $code = Db::name('logistics')->where('code',$msg['ShipperCode'])->find();
            $msg['company']=$code['company'];
            return api_json_echo(true,'结果',$msg,200);
            
      
        
    }

    


}